package com.github.postsanf.yinian.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.EveryDayNoticeEventAdapter;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.entity.YNAlbumResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.google.gson.Gson;
import com.litesuits.http.HttpConfig;
import com.litesuits.http.LiteHttp;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public class EveryDayNoticeEventActivity extends Activity {
    private ListView cardsnyc_listview;
    private Gson gson;
    private BaseApplication application;
    private HttpConfig mHttpConfig;
    private EveryDayNoticeEventAdapter transferEventAdapter;
    private List<YNAlbum> YNAlbumlist = new ArrayList<>();
    private int groupIdMy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_everydaynoticefevent);
        init();
        registerBroadcastReceiver();
        initView();
        getAlbumListById(application.getCurID());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    private void registerBroadcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("noticefinish");
        this.registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    // 广播接收
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "noticefinish":
                    finish();
                    break;
            }
        }
    };

    private void init() {
        gson = new Gson();
        application = (BaseApplication) getApplication();
        mHttpConfig = new HttpConfig(EveryDayNoticeEventActivity.this);
        groupIdMy = application.getCurGid();
    }

    private void initView() {
        cardsnyc_listview = (ListView) findViewById(R.id.cardsnyc_listview);
        findViewById(R.id.transfer_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getAlbumListById(int uId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(uId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowPhoto))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumResponse resGroup = gson.fromJson(s, YNAlbumResponse.class);
                        if (resGroup != null && ReqUtils.isSuccess(resGroup.getCode())) {
                            // 通知列表更新
                            List<YNAlbum> list = resGroup.getData();
                            for (int i = 0; i < list.size(); i++) {
//                                if (list.get(i).getGroupid() != groupIdMy) {
                                YNAlbumlist.add(list.get(i));
//                                }
                            }
                            if (YNAlbumlist.size() > 0) {
                                transferEventAdapter = new EveryDayNoticeEventAdapter(EveryDayNoticeEventActivity.this, YNAlbumlist);
                                cardsnyc_listview.setAdapter(transferEventAdapter);
                                transferEventAdapter.notifyDataSetChanged();
                            } else {
                                Toast.makeText(EveryDayNoticeEventActivity.this, "没有圈子", Toast.LENGTH_SHORT).show();
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        Toast.makeText(EveryDayNoticeEventActivity.this, CommonConstants.YN_NET_LINK, Toast.LENGTH_SHORT)
                                .show();
                    }
                });
        LiteHttp.newApacheHttpClient(mHttpConfig).executeAsync(req);
    }

    // 友盟统计
    public void onResume() {
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
        super.onResume();
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
